package com.huawei.hmsdemo10086.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.huawei.hms.common.util.Logger;
import com.huawei.hms.videokit.player.InitFactoryCallback;
import com.huawei.hms.videokit.player.WisePlayer;
import com.huawei.hms.videokit.player.WisePlayerFactory;
import com.huawei.hms.videokit.player.WisePlayerFactoryOptionsExt;
import com.huawei.hmsdemo10086.POJO.Video;
import com.huawei.hmsdemo10086.R;
import com.huawei.hmsdemo10086.VideoKitApplication;

public class VideoActivity extends AppCompatActivity {

    private String url;
    private WisePlayer player = null;
    private SeekBar seekBar;
    private TextView seek_text;
    private static int time;
    private int progressStart = 0;
    private int isOuto = 1;
    private int isPlay = 0;
    private Handler handler;
    private Boolean isReady = false;
    int i=1;
    private ImageView img_pgbar;
    private AnimationDrawable ad;
    private TextView titleView;
    private TextView timeView;
    private Video video;

    private static final int MAX_PROGRESS =100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        if(!isReady){
            img_pgbar = (ImageView) findViewById(R.id.img_pgbar);
            ad = (AnimationDrawable) img_pgbar.getDrawable();
            img_pgbar.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ad.start();
                }
            }, 100);
        }

        TextView mTitleTextView = (TextView) findViewById(R.id.text_title);
        mTitleTextView.setText("播放视频");
        Button back = (Button)findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(VideoActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

        //url = getIntent().getStringExtra("url");
        video = (Video)getIntent().getSerializableExtra("video");
        url = video.getAddress();

        timeView = findViewById(R.id.video_play_time);
        titleView = findViewById(R.id.video_play_title);
        titleView.setText(video.getTitle());
        timeView.setText(video.getTime());

        player = VideoKitApplication.getWisePlayerFactory().createWisePlayer();

        Button star_pause_button = findViewById(R.id.star_pause_button);
        seekBar = findViewById(R.id.seekBar);
        seekBar.setMax(MAX_PROGRESS);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        if(progressStart>=MAX_PROGRESS){
                        }else{
                            progressStart++;
                            seekBar.incrementProgressBy(1);
                            handler.sendEmptyMessageDelayed(player.isPlaying()?1:0,time/100);
                        }
                        break;
                    case 2:
                        handler.sendEmptyMessageDelayed(player.isPlaying()?1:0,time/100);
                    default:
                        break;
                }
            }
        };
        initSeekbar();

        player.setVideoType(0);
        player.setBookmark(10000);

        player.setPlayUrl(url);




        TextureView textureView = (TextureView) findViewById(R.id.texture_view);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                player.setView(textureView);
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });


        player.ready();
        player.setReadyListener(new WisePlayer.ReadyListener() {
            @Override
            public void onReady(WisePlayer wisePlayer) {
                textureView.setVisibility(View.VISIBLE);
                time = player.getDuration();
                isReady = true;
                img_pgbar.setVisibility(View.INVISIBLE);
            }
        });
        star_pause_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (star_pause_button.getText().toString().equals("播放")){
                    if(isReady){
                        player.start();
                        handler.sendEmptyMessage(player.isPlaying()?1:0);
                        star_pause_button.setText("暂停");
                    }
                }else{
                    if(isReady){
                        player.pause();
                        star_pause_button.setText("播放");
                    }
                }
            }
        });
//
    }

    private void initSeekbar(){

        seek_text = findViewById(R.id.seek_text);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                double toTime = ((double)progress)/100.0*time;
                if(isOuto == 0){
                    player.seek((int) toTime);
                }
                seek_text.setText(progress+"%");
                Log.d("time" , "onProgressChanged: "+player.getCurrentTime());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isOuto = 0;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isOuto = 1;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        player.release();
        handler.removeCallbacksAndMessages(handler);
    }
}